package com.dinzeer.sjapadder.sa;

import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.capability.concentrationrank.ConcentrationRankCapabilityProvider;
import mods.flammpfeil.slashblade.entity.EntityDrive;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.util.VectorHelper;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;

import static com.dinzeer.sjapadder.Config.driveSumRatio;
import static com.dinzeer.sjapadder.Config.driveSumQueenRatio;

public class DriveSum {



   public static void doSlash(LivingEntity playerIn){
       Vec3 centerOffset = Vec3.ZERO;
       Vec3 pos = playerIn.position()
               .add(0.0D, (double) playerIn.getEyeHeight() * 0.75D, 0.0D)
               .add(playerIn.getLookAngle().scale(0.3f));

       pos = pos.add(VectorHelper.getVectorForRotation(-90.0F, playerIn.getViewYRot(0)).scale(centerOffset.y))
               .add(VectorHelper.getVectorForRotation(0, playerIn.getViewYRot(0) + 90).scale(centerOffset.z))
               .add(playerIn.getLookAngle().scale(centerOffset.z));

       // 执行三次均分伤害的攻击
       shootDrive(playerIn, pos, 0, driveSumRatio / 3,90);
       shootDrive(playerIn, pos, -10,driveSumRatio / 3,90);
       shootDrive(playerIn, pos, 10,driveSumRatio / 3,90);
   }




    public static void doSlashForQueen(LivingEntity playerIn){
        Vec3 centerOffset = Vec3.ZERO;
        Vec3 pos = playerIn.position()
                .add(0.0D, (double) playerIn.getEyeHeight() * 0.75D, 0.0D)
                .add(playerIn.getLookAngle().scale(0.3f));

        pos = pos.add(VectorHelper.getVectorForRotation(-90.0F, playerIn.getViewYRot(0)).scale(centerOffset.y))
                .add(VectorHelper.getVectorForRotation(0, playerIn.getViewYRot(0) + 90).scale(centerOffset.z))
                .add(playerIn.getLookAngle().scale(centerOffset.z));

        // 总倍率为 14，前两次各占 2/14，后10次各占 1/14
        shootDrive(playerIn, pos, 0, driveSumQueenRatio / 14 * 2f,90);
        shootDrive(playerIn, pos, 0,driveSumQueenRatio / 14 * 2f,0);
        shootDrive(playerIn, pos, -5, driveSumQueenRatio / 14,45);
        shootDrive(playerIn, pos, -5, driveSumQueenRatio / 14,-45);
        shootDrive(playerIn, pos, 5, driveSumQueenRatio / 14,35);
        shootDrive(playerIn, pos, 5, driveSumQueenRatio / 14,-35);
        shootDrive(playerIn, pos, -10, driveSumQueenRatio / 14,25);
        shootDrive(playerIn, pos, -10, driveSumQueenRatio / 14,-25);
        shootDrive(playerIn, pos, 10, driveSumQueenRatio / 14,15);
        shootDrive(playerIn, pos, 10, driveSumQueenRatio / 14,-15);
        shootDrive(playerIn, pos, -12, driveSumQueenRatio / 14,5);
        shootDrive(playerIn, pos, -12, driveSumQueenRatio / 14,-5);
    }









    public static void shootDrive(LivingEntity playerIn, Vec3 pos, float angleOffset,float damage,float roll) {
        EntityDrive drive = new EntityDrive(SlashBlade.RegistryEvents.Drive, playerIn.level());
        playerIn.level().addFreshEntity(drive);
        drive.setPos(pos.x, pos.y, pos.z);
        drive.setDamage(damage);
        drive.setSpeed(3F);
        float yaw = playerIn.getViewYRot(1.0F) + angleOffset; // 使用更平滑的插值Yaw
        Vec3 direction = VectorHelper.getVectorForRotation(0, yaw); // 水平旋转

        drive.shoot(direction.x,playerIn.getLookAngle().y, direction.z, drive.getSpeed(), 0);

        drive.setOwner(playerIn);
        drive.setRotationRoll(roll);

        int colorcode = playerIn.getMainHandItem()
                .getCapability(ItemSlashBlade.BLADESTATE)
                .map(state -> state.getColorCode()).orElse(0xFF3333FF);
        drive.setColor(colorcode);
        drive.setIsCritical(true);
        drive.setLifetime(30);

        if (playerIn != null)
            playerIn.getCapability(ConcentrationRankCapabilityProvider.RANK_POINT)
                    .ifPresent(rank -> drive.setRank(rank.getRankLevel(playerIn.level().getGameTime())));
    }
}
